package cn.unknowpupil.init.controller.monitor;


import cn.unknowpupil.init.common.enums.BusinessType;
import cn.unknowpupil.init.common.resopnse.ResultResponse;
import cn.unknowpupil.init.components.annotation.Log;
import cn.unknowpupil.init.domain.dto.LoginUser;
import cn.unknowpupil.init.domain.system.SysLogRecord;
import cn.unknowpupil.init.domain.system.SysLoginInfo;
import cn.unknowpupil.init.service.system.ISysLogRecordService;
import cn.unknowpupil.init.service.system.ISysUserService;
import cn.unknowpupil.init.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created at 2020-10-16 by  w10g <br>
 * Initialize LogRecord  ... <br>
 * 操作日志记录 视图浏览层处理
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Api(tags = {"系统-操作日志记录模块"}, value = "LogRecord")
@RestController
@RequestMapping(value = "admin/log-record")
public class SysLogRecordController {

    @Resource
    private ISysLogRecordService sysLogRecordService;
    @Resource
    private ISysUserService userService;


    /**
     * 获取
     *
     * @param LogRecordId {@code Long}
     * @return {@link ResultResponse}
     */
    @Log(title = "获取", businessType = BusinessType.OTHER)
    @GetMapping("get/{LogRecordId}")
    public ResultResponse get(@PathVariable("LogRecordId") Long LogRecordId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        System.out.println("loginUser" + loginUser);
        SysLogRecord LogRecord = sysLogRecordService.info(LogRecordId);
        return ResultResponse.buildSuccess(LogRecord);
    }

    /**
     * 删除
     *
     * @param ids {@code Long}
     * @return {@link ResultResponse}
     */
    @DeleteMapping("remove/{ids}")
    public ResultResponse remove(@PathVariable("ids") Long[] ids) {
        // 业务逻辑
        boolean deleted = sysLogRecordService.delete(ids);
        if (deleted) {
            return ResultResponse.success("删除成功");
        } else {
            return ResultResponse.buildFailure("删除失败");
        }
    }

    /**
     * 分页
     *
     * @param current {@code int} 页码
     * @param size    {@code int} 笔数
     * @return {@link ResultResponse}
     */
    @GetMapping("page/{current}/{size}")
    public ResultResponse<IPage<SysLogRecord>> page(@PathVariable("current") int current, @PathVariable("size") int size, @ModelAttribute SysLogRecord LogRecord) {
        IPage<SysLogRecord> page = sysLogRecordService.page(current, size, LogRecord);
        return ResultResponse.buildSuccess(page);
    }

    /**
     * 分页
     *
     * @param current {@code int} 页码
     * @param size    {@code int} 笔数
     * @return {@link ResultResponse}
     */
    @GetMapping("byUser/{current}/{size}")
    public ResultResponse<IPage<SysLogRecord>> listOperLogByUser(@PathVariable("current") int current, @PathVariable("size") int size, @ModelAttribute SysLogRecord LogRecord) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        LogRecord.setOperName(loginUser.getUsername());
        IPage<SysLogRecord> page = sysLogRecordService.page(current, size, LogRecord);
        return ResultResponse.buildSuccess(page);
    }


    @Log(title = "清空操作日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public ResultResponse clean() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        boolean remove = false;
        if (SecurityUtils.isSuperAdmin(loginUser.getPid())) {

            remove = sysLogRecordService.remove(null);
        } else if (SecurityUtils.isAdmin(loginUser.getPid())) {
            List<String> sunUser = userService.getAllSunUserByUserId(loginUser.getId());
            sunUser.add(loginUser.getUsername());
            LambdaQueryWrapper<SysLogRecord> query = new LambdaQueryWrapper<>();
            query.in(SysLogRecord::getOperName, sunUser);
            remove = sysLogRecordService.remove(query);
        } else {
            return ResultResponse.buildFailure("您没有权限");
        }
        if (remove) {
            return ResultResponse.success("清除成功");
        } else {
            return ResultResponse.buildFailure("清除失败");
        }
    }
}
